package key;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Log {
	/**
	 * 用于生成日志文件的Log类
	 * @return 
	 */
	
	File logFile = null;
	FileWriter fw = null;
	PrintWriter pw =null;
	SimpleDateFormat clo = null;
	Date d = null;
	
	public Log(){
		
		d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		try{//生成/找到文件
			logFile = new File("C:\\log\\"+sdf.format(d)+".txt");
			fw = new FileWriter(logFile, true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		pw = new PrintWriter(fw);
	}
	
	public void writeIn(String inf){//写入文件
		d = new Date();
		clo = new SimpleDateFormat("HH:mm:ss");
		pw.println(clo.format(d)+"  "+inf);
		pw.flush();
	}
	
	public void close(){//关闭流
		try {
			fw.flush();
			pw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
